module KingdomsHelper
  def options_kingdom_select (selected = 0)
    options_records_select Kingdom.find(:all), selected
  end
  
  def options_realm_select (selected = "")
    map = Hash.new
    kingdoms = Kingdom.find(:all)
    if kingdoms
      kingdoms.each do |kingdom|
        map["kingdom:#{kingdom.id}"] = kingdom.name
        duchies = kingdom.duchies
        if duchies
          duchies.each do |duchy|
            map["duchy:#{duchy.id}"] = kingdom.name + " : " + duchy.name
            counties = duchy.counties
            if counties
              counties.each do |county|
                map["county:#{county.id}"] = kingdom.name + " : " + duchy.name + " : " + county.name
                towns = county.towns
                if towns
                  towns.each do |town|
                    map["town:#{town.id}"] = kingdom.name + " : " + duchy.name + " : " + county.name + " : " + town.name
                  end
                end
              end
            end
          end
        end
      end
    end
    options_hash_select map.sort{|a,b| a[1]<=>b[1]}, selected
  end
  
  def is_court_ruler? (court)
    return false unless current_player
    return false unless current_player.player_character
    return false unless court
    ruler = court.ruler
    return false unless ruler
    ruler.id == current_player.player_character.id
  end
  
end
